home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / xcmd / dartxcmd.sit / Dartmouth XCMD's 3.1 / card_16890.txt < prev    next >
Encoding:
Text File  |  1989-06-02  |  13.0 KB  |  440 lines

  1. -- card: 16890 from stack: in.1
  2. -- bmap block id: 17455
  3. -- flags: 0000
  4. -- background id: 16896
  5. -- name: Replace
  6. ----- HyperTalk script -----
  7. on opencard
  8.   hide card field "source"
  9.   put empty into card field "new"
  10.   pass opencard
  11. end opencard
  12.  
  13. on Install
  14.   put ChooseTargetStack() into it
  15.   InstallResource XFCN,Replace,it
  16. end Install
  17.  
  18.  
  19.  
  20. -- part 2 (field)
  21. -- low flags: 01
  22. -- high flags: 0007
  23. -- rect: left=305 top=58 right=170 bottom=478
  24. -- title width / last selected line: 0
  25. -- icon id / first selected line: 0 / 0
  26. -- text alignment: 0
  27. -- font id: 3
  28. -- text size: 9
  29. -- style flags: 0
  30. -- line height: 12
  31. -- part name: old
  32.  
  33.  
  34. -- part 3 (button)
  35. -- low flags: 00
  36. -- high flags: 8004
  37. -- rect: left=50 top=308 right=324 bottom=225
  38. -- title width / last selected line: 0
  39. -- icon id / first selected line: 0 / 0
  40. -- text alignment: 1
  41. -- font id: 0
  42. -- text size: 12
  43. -- style flags: 0
  44. -- line height: 16
  45. -- part name: Replace Case Sensitive
  46. ----- HyperTalk script -----
  47. on mouseUp
  48.   put empty into card field "new"
  49.   put Replace(card field "old","mytest","myTest",true) into card field "new"
  50.   set the cursor to hand
  51. end mouseUp
  52.  
  53.  
  54.  
  55. -- part 6 (button)
  56. -- low flags: 00
  57. -- high flags: 8003
  58. -- rect: left=304 top=299 right=321 bottom=425
  59. -- title width / last selected line: 0
  60. -- icon id / first selected line: 0 / 0
  61. -- text alignment: 1
  62. -- font id: 0
  63. -- text size: 12
  64. -- style flags: 0
  65. -- line height: 16
  66. -- part name: Show LSC Source
  67. ----- HyperTalk script -----
  68. on mouseUp
  69.   get the visible of card field "source"
  70.   set the visible of card field "source" to not it
  71.   if it is false then
  72.     set the name of me to "Hide LSC Source"
  73.   else
  74.     set the name of me to "Show LSC Source"
  75.   end if
  76. end mouseUp
  77.  
  78.  
  79.  
  80. -- part 8 (field)
  81. -- low flags: 01
  82. -- high flags: 0007
  83. -- rect: left=305 top=181 right=292 bottom=478
  84. -- title width / last selected line: 0
  85. -- icon id / first selected line: 0 / 0
  86. -- text alignment: 0
  87. -- font id: 3
  88. -- text size: 9
  89. -- style flags: 0
  90. -- line height: 12
  91. -- part name: new
  92.  
  93.  
  94. -- part 9 (field)
  95. -- low flags: 01
  96. -- high flags: 2007
  97. -- rect: left=18 top=33 right=288 bottom=290
  98. -- title width / last selected line: 0
  99. -- icon id / first selected line: 0 / 0
  100. -- text alignment: 0
  101. -- font id: 3
  102. -- text size: 10
  103. -- style flags: 0
  104. -- line height: 13
  105. -- part name: Documentation
  106.  
  107.  
  108. -- part 7 (field)
  109. -- low flags: 81
  110. -- high flags: 0007
  111. -- rect: left=18 top=31 right=290 bottom=489
  112. -- title width / last selected line: 0
  113. -- icon id / first selected line: 0 / 0
  114. -- text alignment: 0
  115. -- font id: 3
  116. -- text size: 10
  117. -- style flags: 0
  118. -- line height: 13
  119. -- part name: Source
  120.  
  121.  
  122. -- part 10 (button)
  123. -- low flags: 00
  124. -- high flags: 8004
  125. -- rect: left=50 top=292 right=308 bottom=225
  126. -- title width / last selected line: 0
  127. -- icon id / first selected line: 0 / 0
  128. -- text alignment: 1
  129. -- font id: 0
  130. -- text size: 12
  131. -- style flags: 0
  132. -- line height: 16
  133. -- part name: Replace Case Insensitive
  134. ----- HyperTalk script -----
  135. on mouseUp
  136.   put empty into card field "new"
  137.   put Replace(card field "old","mytest","myTest",false) into card field "new"
  138.   set the cursor to hand
  139. end mouseUp
  140.  
  141.  
  142.  
  143. -- part contents for card part 2
  144. ----- text -----
  145. on test
  146.    global myTest
  147.    put mytest + 1 into MYTest
  148.    if MYTEST > 100 then
  149.       beep
  150.       put 0 into MyTest
  151.   end if
  152. end test
  153.  
  154. -- part contents for card part 7
  155. ----- text -----
  156. /* Replace1.2d0.c */
  157. /* ┬⌐ Trustees of Dartmouth College */
  158. /* written in LightSpeed C  ┬⌐ Think Technologies, Inc */
  159. /* by Roger Brown 7/21/88  Courseware Development Group */
  160.  
  161. /* version 1.2d0 is invisible replace only, using off-screen TextEdit */
  162. /* This uses TextEdit to do the editing, which is not the most efficient
  163.    method, but it is a precursor to a later version that will display
  164.    the text as it is edited and allow uer verification. */
  165.    
  166. /* This is a HyperCard XFCN that locates a target string in an input
  167.    container, replaces the string with another string and returns the 
  168.    edited result. Case sensitivity is an optional third parameter.
  169.      
  170.    Syntax is:
  171.    
  172.         get Replace(container,oldString,newString,caseSensitive)
  173.    
  174.                 ex. put Replace(card field 1,"myGlobal","myOtherGlobal",false)
  175.                         into card field 1
  176.   
  177.    returns: a copy of the input container with all occurrences of newString replaced
  178.             by oldString.
  179.    
  180.    where    container   is any hypercard container (field, variable), presumed to be 
  181.                         multi-lined.
  182.             oldString   is the string to look for
  183.             newString   is the string to replace oldString with   
  184.             caseSensitive is an optional parameter turning on case sensitivity   
  185.                                   
  186.       To compile: create a project with this and MacTraps. Build as code resource type
  187.                   XFCN named Replace.
  188. */
  189.  
  190.  
  191.  
  192. #include "stddata_ctype.c"
  193. #include "strings.c"
  194. #include "ToolboxUtil.h"
  195. #include "QuickDraw.h"
  196. #include "TextEdit.h"
  197. #include "HyperXCmd.h"
  198. #include "XCmdGlue.inc.c"
  199. #include "SetUpA4.h"
  200.  
  201. #define FALSE 0
  202. #define TRUE !FALSE
  203. #define MAXINT 32768
  204.  
  205. /* globals */
  206.  
  207. Handle theContainer;                       /* ahdnle to the original container */
  208. char *oldString;                                                   /* pointer to the string to replace */
  209. char *newString;                           /* pointer to the string to replace with */
  210. Boolean caseSensitive;                     /* flag - case sensitive match */
  211. Handle resultH;                            /* handle to the edited data */
  212. XCmdBlockPtr   gParamPtr;
  213. TEHandle theTEH;
  214.         
  215. /* change a string to all upper case */
  216.  
  217. int toupper(c)
  218. char    c;
  219. {
  220.     return( (c>='a')&&(c<='z') ? (c-('a'-'A')) : c );
  221. }
  222.  
  223. /* change a string to all upper case */
  224.  
  225. ucase(s)
  226. char *s;
  227. {
  228.         int i;
  229.         char c;
  230.         
  231.         for (i=0;i<strlen(s);i++) {
  232.                 s[i] = toupper(s[i]);
  233.         }
  234. }
  235.  
  236. /* see if two characters match, maybe ignoring case */
  237.  
  238. matchChar(a,b,theCase)
  239. char a,b;
  240. Boolean theCase;
  241. {
  242.     if (theCase==TRUE) return (a==b);
  243.     return (toupper(a)==toupper(b));
  244. }
  245.  
  246. /* find the target as specified, return start byte */
  247.  
  248. FindIt(theField,theTarget,caseSens,start)
  249. char *theField;          /* pointer to field to search */
  250. char *theTarget;         /* pointer to target string */
  251. Boolean caseSens;       
  252. int start;                      /*  start is where (byte) to start */
  253. {
  254.         int c;             /* index of current character */
  255.         int tLen;          /* target length */
  256.         int line;          /* line currently being searched */
  257.         int lPos;                  /* character position in line */
  258.         register char *fc,*tc; /* pointers to field and target characters */
  259.         
  260.         tLen = strlen(theTarget);            /* assume this is short */
  261.         c = lPos = 0; 
  262.         line = 1;                            /* offset into theField */
  263.         fc = theField;                       /* pointer into theField */
  264.         while (*fc != 0) {                   /* scan to end */
  265.                 tc = theTarget;                  /* pointer to theTarget */
  266.                 if (*fc==13) {
  267.                          line++;                     /* passing a line boundary */
  268.                          lPos = -1;                  /* reset position */
  269.                 }
  270.                 if ((c) >= start) {             /* don't go until something matches */
  271.                         while (matchChar(*tc,*fc,caseSens)) {   /* check for match */
  272.                                 fc++;                      /* cycle through matching characters */
  273.                                 tc++;
  274.                                 if (tc == (theTarget+tLen)){   /* all target characters matched */
  275.                                          return c;  /* matched */
  276.                                 }
  277.                         }
  278.                 }
  279.                 c++;                          /* move along */
  280.                 lPos++;
  281.                 fc = theField + c;            /* reset pointer after a search */
  282.         }
  283.         return -1;                         /* target not found */
  284. }
  285.  
  286. /* replace all occurences of the text without showing the user */
  287.  
  288. ReplaceInvisible()
  289. {
  290.         Rect aRect;
  291.         int selStart,selEnd;
  292.         int oPos,nLen,oLen;
  293.         GrafPtr curPort,tempPort;
  294.         Str255 tempText;
  295.         
  296.         /* we are going to use TextEdit to do this and TextEdit wants to be in a port
  297.                 so we are going to save the current port, create a new one and move it
  298.                 off screen, do the replacing, and restore the original port. */
  299.                 
  300.         GetPort(&curPort);                     /* save to restore later */
  301.         
  302.         /* open an offscreen graph port */
  303.         tempPort = (GrafPtr)NewPtr(sizeof(GrafPort));
  304.         OpenPort(tempPort);
  305.         MovePortTo(-MAXINT,-MAXINT);
  306.         
  307.         /* create a TextEdit record */
  308.         SetRect(&aRect,-MAXINT,-MAXINT,-MAXINT+200,-MAXINT+200);
  309.         theTEH = TENew(&aRect,&aRect);
  310.         (**theTEH).inPort = tempPort;
  311.  
  312.         oLen = strlen(oldString);
  313.         nLen = strlen(newString);
  314.         
  315.         /* initialize the TERecord with the container text */
  316.         
  317.         TESetText(*theContainer,(long)strlen(*theContainer),theTEH);
  318.         
  319.         /* do the replacing */
  320.         
  321.         oPos = 0;
  322.         selStart = FindIt(*(**theTEH).hText,oldString,caseSensitive,oPos);
  323.         while (selStart != -1) {
  324.                 selEnd = selStart + oLen;
  325.                 TESetSelect(selStart,selEnd,theTEH);
  326.                 TECut(theTEH);
  327.                 TEInsert(newString,(long)nLen,theTEH);
  328.                 oPos = (**theTEH).selEnd;
  329.                 selStart = FindIt(*(**theTEH).hText,oldString,caseSensitive,oPos);
  330.         }
  331.         
  332.         /* return the edited text as the result */
  333.         resultH = NewHandle((**theTEH).teLength+1);
  334.         BlockMove((*(**theTEH).hText),*resultH,(**theTEH).teLength);
  335.         *(*resultH+(**theTEH).teLength) = 0;
  336.         
  337.         /* dispose the port and restore the original */
  338.         
  339.         ClosePort(tempPort);
  340.         SetPort(curPort);
  341.         DisposPtr(tempPort);
  342.         
  343. }
  344.  
  345. /* XCMD entry */
  346.  
  347. pascal void Replace(paramPtr)
  348. XCmdBlockPtr    paramPtr;
  349. {   
  350.     int i;
  351.     Str255 paramStr;
  352.     Ptr casePtr,verPtr;
  353.     CursHandle theCursor;
  354.      
  355.     gParamPtr = paramPtr;
  356.     
  357.     /* put up a watch */
  358.     
  359.     theCursor = GetCursor(watchCursor);
  360.     SetCursor(*theCursor);
  361.     
  362.     /* lock parameters down so we can point to themn */
  363.     
  364.     for (i=0;i<paramPtr->paramCount;i++) {
  365.         MoveHHi(paramPtr->params[i]);
  366.         HLock(paramPtr->params[i]);
  367.     }
  368.     
  369.     /* get the parameters */
  370.     
  371.     theContainer = paramPtr->params[0];   /* the container */
  372.     oldString = *(paramPtr->params[1]);  /* the target string */
  373.     newString = *(paramPtr->params[2]);  /* the replace string */
  374.     
  375.     if (paramPtr->paramCount>3)  {       /* case sensitivity flag */
  376.         casePtr = *(paramPtr->params[3]);
  377.         strcpy(paramStr,casePtr);
  378.     }
  379.     else strcpy(paramStr,"");
  380.     ucase(paramStr);
  381.         if (strcmp(paramStr,"TRUE")==0) 
  382.         caseSensitive = TRUE;
  383.     else caseSensitive = FALSE;
  384.   
  385.     ReplaceInvisible();
  386.     
  387.     /* assemble the return string */
  388.     
  389.     paramPtr->returnValue = resultH;
  390.     for (i=0;i<paramPtr->paramCount;i++) HUnlock (paramPtr->params[i]);
  391.     return;
  392. }
  393.  
  394. pascal void main(paramPtr)
  395. XCmdBlockPtr    paramPtr;
  396. /* this is the entry point for the XFCN */
  397. {
  398.         RememberA0();
  399.         SetUpA4();                  /* to access globals */
  400.         Replace(paramPtr);     
  401.         RestoreA4(); 
  402.         return;
  403. }
  404.  
  405.  
  406.  
  407. -- part contents for card part 9
  408. ----- text -----
  409. Replace version 1.2d0.c
  410. Roger Brown
  411.  
  412. Replace is an XFCN that replaces all occurrences of specified target text with new text in a HyperCard container.  The input is a container,
  413. the old text, the new text, and a case sensitivity flag. The output is an edited copy of the container data.
  414.  
  415. This version does not support wildcards or pattern matching, and does not allow the user to verify each replacement.  We plan to add these features in a later version, so the source for this utilizes toolbox TextEdit to do the replacement in an off-screen port.  This is not the most efficient method for this particular XFCN, but it serves as a precursor to later developement.  The operation is still fairly fast.
  416.  
  417. This XFCN is best used as part of scripts that identify the containers (scripts, fields, globals) to be edited.  For example, the SearchScript utility shipped with HyperCard could be modified to be a ReplaceScript utility by replacing "edit the script of" with this XFCN.
  418.  
  419. INVOKING Replace
  420.  
  421.   get Replace(container,oldText,newText,
  422.           caseSens)
  423.         
  424.    where   container is any HyperCard container
  425.                 oldText is the text to be replaced
  426.                 newText is the text to replace with
  427.                 caseSens is an option flag indicating if
  428.                     the replcaement will be case sensitive                                    
  429.  
  430. EXAMPLE
  431.  
  432.  ex. put Replace(the script of this card,"
  433.               myglobal","MyGlobal",false) into the 
  434.               script of this card)
  435.       
  436. This example will correct the upper case appearance of all references to MyGlobal.
  437.  
  438.  
  439. REVISION HISTORY
  440. 1.2d0.c First public release. Non-verify version.